package com.wswy.wzcx.ui.car.result;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.airbnb.lottie.LottieAnimationView;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.wswy.wzcx.R;
import timber.log.Timber;

public class QueryLoadingHeader extends FrameLayout implements RefreshHeader {

  private static final String TAG = "QueryLoadingHeader";

  private static final String[] tips = {"正在连接交管数据...","请求验证车辆信息...","尝试获取违法数据..."};

  public QueryLoadingHeader(
      @NonNull Context context) {
    super(context,null);
  }

  public QueryLoadingHeader( @NonNull Context context,
      @Nullable AttributeSet attrs) {
    this(context, attrs,0);
  }

  public QueryLoadingHeader( @NonNull Context context,
       @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initViews(context);
  }

  private ImageView imgLoadingView;
  private LottieAnimationView animLoadingView;

  private TextView tvTitle;
  private TextView tvTime;

  private AnimatorSet currentAnimSet;
  private float mTitleY = 0f;


  private void initViews(Context context){


    View contentLayout = LayoutInflater.from(context).inflate(R.layout.layout_query_head_loading,this,false);


    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;

    addView(contentLayout,layoutParams);


    imgLoadingView = findViewById(R.id.img_loading);
    animLoadingView = findViewById(R.id.img_anim_loading);


    tvTitle = findViewById(R.id.tv_loading_title);
    tvTime = findViewById(R.id.tv_loading_time);

  }


  @NonNull
  @Override
  public View getView() {
    return this;
  }

  @NonNull
  @Override
  public SpinnerStyle getSpinnerStyle() {
    return SpinnerStyle.Translate;
  }

  @Override
  public void setPrimaryColors(int... colors) {

  }

  @Override
  public void onInitialized(@NonNull RefreshKernel kernel, int height, int extendHeight) {

  }

  @Override
  public void onMoving(boolean isDragging, float percent, int offset, int height,
      int maxDragHeight) {

    if (isDragging){
      animLoadingView.setVisibility(GONE);

      imgLoadingView.setVisibility(VISIBLE);
      tvTime.setVisibility(VISIBLE);

      if (percent >= 1) {
        imgLoadingView.setRotation(180);
        tvTitle.setText("释放刷新");
      } else {

        imgLoadingView.setRotation(180*percent);
        tvTitle.setText("下拉刷新");
      }
    }

  }

//  @Override
//  public void onPulling(float percent, int offset, int height, int extendHeight) {
//    //Log.e(TAG, "onPulling --> percent:"+percent+" offset:"+offset+"  ");
//
//    animLoadingView.setVisibility(GONE);
//
//    imgLoadingView.setVisibility(VISIBLE);
//    tvTime.setVisibility(VISIBLE);
//
//    if (percent >= 1) {
//      imgLoadingView.setRotation(180);
//      tvTitle.setText("释放刷新");
//    } else {
//
//      imgLoadingView.setRotation(180*percent);
//      tvTitle.setText("下拉刷新");
//    }
//
//  }


  @Override
  public void onReleased(RefreshLayout refreshLayout, int height, int extendHeight) {
    Log.e(TAG, "onReleased --> height:"+height);
  }

  @Override
  public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int extendHeight) {

    Log.e(TAG, "onStartAnimator --> ");

    tvTime.setVisibility(GONE);
    animLoadingView.playAnimation();

    startTips();

  }

  @Override
  public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
    return 0;
  }

  @Override
  public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

  }

  @Override
  public boolean isSupportHorizontalDrag() {
    return false;
  }

  @Override
  public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState,
      RefreshState newState) {

    switch (newState){
      case None:
      case PullDownToRefresh:

        canAnim = false;


        tvTime.setVisibility(VISIBLE);

        tvTitle.setVisibility(VISIBLE);
        tvTitle.setAlpha(1f);
        tvTitle.setY(0f);

        break;
      case Refreshing:
        imgLoadingView.setVisibility(GONE);
        animLoadingView.setVisibility(VISIBLE);

        canAnim = true;

        break;
      case RefreshFinish:

        animLoadingView.cancelAnimation();
        animLoadingView.setVisibility(GONE);

        canAnim = false;


        tvTitle.setVisibility(VISIBLE);
        tvTitle.setAlpha(1f);
        tvTitle.setY(0f);

        if (currentAnimSet != null && currentAnimSet.isRunning()){
          currentAnimSet.end();
        }
        break;

    }

    //Log.e(TAG, "onStateChanged --> oldState:"+oldState+"    newState:"+newState);
  }



  private int tipIdx = 0;
  private boolean canAnim = false;

  private void startTips(){
    if (!canAnim){
      return;
    }

    if (tipIdx >= tips.length){
      tipIdx = 0;
    }

    if (mTitleY < 1f){
      mTitleY = tvTitle.getHeight();
    }

    //Timber.e("tips ---->>>>  "+tipIdx+"   "+mTitleY+"   "+tvTitle.getHeight());

    tvTitle.setText(tips[tipIdx]);



    AnimatorSet animatorSet = new AnimatorSet();


    ObjectAnimator fadeInUpAnimator = ObjectAnimator.ofPropertyValuesHolder(tvTitle,
        PropertyValuesHolder.ofFloat(View.ALPHA, 0f, 1f),
        PropertyValuesHolder.ofFloat(View.Y, mTitleY, 0f)
    );

    fadeInUpAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
    fadeInUpAnimator.setDuration(300);



    ObjectAnimator holderAnimator = ObjectAnimator.ofFloat(tvTitle, View.ALPHA, 1f, 0.99f);
    holderAnimator.setDuration(1500);


    ObjectAnimator fadeOutUpAnimator = ObjectAnimator.ofFloat(tvTitle, View.Y, 0f, -10f);
    fadeOutUpAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
    fadeOutUpAnimator.setDuration(100);

    ObjectAnimator fadeOutDownAnimator = ObjectAnimator.ofPropertyValuesHolder(tvTitle,
        PropertyValuesHolder.ofFloat(View.ALPHA, 1f, 0f),
        PropertyValuesHolder.ofFloat(View.Y, -10f, mTitleY)
    );
    fadeOutDownAnimator.setInterpolator(new DecelerateInterpolator());
    fadeOutDownAnimator.setDuration(300);



    animatorSet.addListener(new AnimatorListenerAdapter() {
      @Override
      public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        tipIdx ++;

        startTips();

      }
    });

    animatorSet.playSequentially(fadeInUpAnimator,holderAnimator,fadeOutUpAnimator,fadeOutDownAnimator);
    animatorSet.start();


    currentAnimSet = animatorSet;

  }



  public void setLastRefreshTime(String timeStr){
    tvTime.setText(timeStr);
  }

}
